package com.rbcs.reimb.service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rbcs.borrow.servive.BorrowManager;
import com.rbcs.flow.FlowConstants;
import com.rbcs.reimb.model.ReimbCount;
import com.systop.core.Constants;
import com.systop.core.service.BaseGenericsManager;

/**
 * 借款报销统计管理Manager
 * 
 * @author ShangHua
 */
@Service
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ReimbCountManager extends BaseGenericsManager<ReimbCount> {

	//借款管理Manager
	@Autowired
	private BorrowManager borrowManager;
	//报销管理Manager
	@Autowired
	private ReimbManager reimbManager;

	
	/**
	 * 借款汇总金额
	 * @param userId
	 * @param deptId
	 * @param projectId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public double getBorrowAmount(Integer userId, Integer deptId, Integer projectId, Date startDate, Date endDate) {
		StringBuffer hql = new StringBuffer("select sum(amount) from Borrow b where 1 = 1 ");
		List args = new ArrayList();
		hql.append(" and b.audStatus = ? and b.payStatus = ? ");
		args.add(FlowConstants.AUDIT_PASS);
		args.add(Constants.YES);
		if (userId != null) {
			hql.append("and b.borrower.id = ? ");
			args.add(userId);
		}
		if (deptId != null) {
			hql.append("and b.dept.id = ? ");
			args.add(deptId);
		}
		if (projectId != null) {
			hql.append("and b.project.id = ? ");
			args.add(projectId);
		}
		
		if (startDate != null) {
			hql.append("and b.createDate >= ? ");
			args.add(startDate);
		}
		if (endDate != null) {
			hql.append("and b.createDate <= ? ");
			args.add(endDate);
		}
		
		List list = borrowManager.query(hql.toString(), args.toArray());
		double borrowAmount = 0;
		for (Object object : list) {
			if(object != null){
				borrowAmount = Float.valueOf(object.toString());
			}
		}	
		return borrowAmount;
	}
	
	/**
	 * 报销汇总金额
	 * @param userId
	 * @param deptId
	 * @param projectId
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public double getReimbAmount(Integer userId, Integer deptId, Integer projectId, Date startDate, Date endDate) {
		StringBuffer hql = new StringBuffer("select sum(amount) from Reimb r where r.parentReimb is null ");
		List args = new ArrayList();
		hql.append(" and r.payStatus = ?");
		args.add(Constants.YES);
		if (userId != null) {
			hql.append("and r.reimber.id = ? ");
			args.add(userId);
		}
		if (deptId != null) {
			hql.append("and r.dept.id = ? ");
			args.add(deptId);
		}
		if (projectId != null) {
			hql.append("and r.project.id = ? ");
			args.add(projectId);
		}
		
		if (startDate != null) {
			hql.append("and r.createDate >= ? ");
			args.add(startDate);
		}
		if (endDate != null) {
			hql.append("and r.createDate <= ? ");
			args.add(endDate);
		}
		
		List list = reimbManager.query(hql.toString(), args.toArray());
		double reimbAmount = 0;
		for (Object object : list) {
			if(object != null){
				reimbAmount = Float.valueOf(object.toString());
			}
		}	
		return reimbAmount;
	}
}
